<template>
  <div class="authorization">
    <!-- -->
    <div class="con">
      
      <div class="iteminput">
              <div class="leftname"> chipid ：</div>
              <!-- <el-input v-model="chipid"  style="width:240px"></el-input> -->
              <div ref="textToCopy" class="wenbentxt">{{chipid}}</div>
              <el-button type="primary" style="margin-left:12px" @click="fasongfn('chipid')">查询</el-button>
              <el-button type="primary" style="margin-left:12px" @click="copyText()">复制</el-button>
        </div>

      <div class="iteminput">
              <div class="leftname"> SN码 ：</div>
              <el-input v-model="snma" placeholder="请输入SN码" style="width:240px"></el-input>
              <el-button type="primary" style="margin-left:12px" @click="fasongfn('SN')">确定</el-button>
        </div>
        <div class="iteminput">
              <div class="leftname">授权码：</div>
              <el-input v-model="shouquanma" placeholder="请输入授权码" style="width:240px"></el-input>
              <el-button type="primary" style="margin-left:12px" @click="fasongfn('授权码')">确定</el-button>
        </div>
        <div class="el-loading-mask" v-show="isMask">
             <div class="el-loading-spinner">
				<svg viewBox="25 25 50 50" class="circular"> <circle cx="50" cy="50" r="20" fill="none" class="path"></circle></svg>
				<div>正在重新启动...</div>
				</div>
          </div>
    </div>
  </div>
</template>

<script>
import {authconfigsavesncode,authconfigsaveaucode,authconfigchipId} from "../api/dataComparisonApi";
import {restartSystem,pingSystem} from "../api/forwardApi";
export default {
  data() {
    return {
      shouquanma:"",
      snma:"",
      chipid:"",
      isMask:false
    };
  },

  mounted() {
    
  },

  methods: {
     async copyText() {
      try {
        await navigator.clipboard.writeText(this.$refs.textToCopy.textContent);
        this.$message({showClose: true,message: '文本已复制到剪贴板',type: 'success'});
      } catch (err) {
        console.error('复制失败，请手动复制');
        this.fallbackCopyText(); // 如果Clipboard API不可用，使用备用方法
      }
    },
    fallbackCopyText() {
      const textarea = document.createElement('textarea');
      textarea.value = this.$refs.textToCopy.textContent;
      document.body.appendChild(textarea);
      textarea.select();
      
      try {
        document.execCommand('copy');
        this.$message({showClose: true,message: '文本已复制到剪贴板',type: 'success'});
      } catch (err) {
        console.error('复制失败，请手动复制');
      }
      
      document.body.removeChild(textarea);
    },
     fasongfn(type){
       if(type == 'SN'){
          if(this.snma === ""){
            this.$message({showClose: true,message: 'SN码不能为空',type: 'warning'});
            return
          }
          var dd = {
             'sn_code':this.snma
          }
          this.$confirm(`确定要发送SN码吗？`,'提示',{
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                this.isMask = true
                authconfigsavesncode(dd).then(res => {
                  //  console.log("发送sn码",res);
                  this.isMask = false
                  if(res != undefined && res.code == 200){
                      this.$message({showClose: true,message: '操作成功',type: 'success'});
                  }else {
                      this.$message({showClose: true,message: '操作失败',type: 'error'});
                  }
              })
            }).catch(() => {
                  // ...
            });
           
       }else if(type == '授权码'){
         if(this.shouquanma === ""){
            this.$message({showClose: true,message: '授权码不能为空',type: 'warning'});
            return
          }
          var dd = {
             'au_code':this.shouquanma
          }
          this.$confirm(`确定要发送授权码吗？`,'提示',{
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                this.isMask = true
                authconfigsaveaucode(dd).then(res => {
                    //  console.log("发送授权码",res);
                    this.isMask = false
                    if(res != undefined && res.code == 200){
                        this.Restartfn()
                        // this.$message({showClose: true,message: '操作成功',type: 'success'});
                    }else {
                      this.$message({showClose: true,message: '操作失败',type: 'error'});
                    }
                })
            }).catch(() => {
                  // ...
            });
           
       }else if(type == 'chipid'){
           authconfigchipId().then(res => {
               if(res && res.chip_id){
                  this.$message({showClose: true,message: '查询成功',type: 'success'});
                  this.chipid = res.chip_id
               }
           })
       }
     },
     //重启网关
      Restartfn(){
            this.$confirm(`操作成功，重启网关才能生效，是否要重启网关吗？`,'提示',{
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
            //   this.$message({showClose: true,message: '删除成功',type: 'success'});
            //    this.$message.error('删除失败！！')
            this.isMask = true
            //  融合版为1，纯净版为2
            restartSystem({'model':1}).then(res => {})
            setTimeout(() => {
              this.viewChongqi()
            },10000) 
            }).catch(() => {
                  // ...
            });
      },
      //检查管理机是否重启完成
      viewChongqi(){
        pingSystem().then(res => {
          // console.log("检查管理机心跳",res);
          if(res.code == 200){
            this.isMask = false
            this.$message({showClose: true,message: '重启成功',type: 'success'});
            //   window.location.reload();
          }else {
            setTimeout(() => {
              this.viewChongqi()
            },2000)
          }
        }).catch(() => {
          setTimeout(() => {
            this.viewChongqi()
          },2000)
        })
	 }

  },
};
</script>
<style lang="scss" scoped>
    .authorization {
         width: 100%;
         height: 100%;
         .con {
             width: 100%;
             height: 100%;
             padding: 12px 20px;
             box-sizing: border-box;
             background-color: #fff;
             position: relative;
             .iteminput {
                  display: flex;
                  align-items: center;
                  margin-top: 30px;
             }
             .leftname {
               width: 70px;
               text-align: right;
               font-size: 14px;
             }
             .wenbentxt{
               min-width: 240px;
               height: 38px;
               font-size: 13px;
               border: 1px solid #dcdfe6;
               display: flex;
               align-items: center;
               box-sizing: border-box;
               border-radius: 4px;
               user-select: text;
               padding: 0px 11px;
             }
         }
    }
</style>